package cqrtplm.controller;


import com.github.pagehelper.PageInfo;
import com.hustcad.plm.pdm.classification.model.vo.TyplmClassificationEntityTreeVO;
import com.hustcad.plm.pdm.code.service.impl.number.TyplmClassCodeCheckExtendServiceImpl;
import com.hustcad.plm.pdm.enumeration.model.vo.LocalValueVO;
import com.hustcad.plm.pdm.enumeration.service.TyplmLocalePropertyValueService;
import com.hustcad.plm.pdm.type.model.vo.TypeTree;
import com.ty.basic.response.ResponseResult;
import cqrtplm.constant.RTErrorCodeEnum;
import cqrtplm.constant.RouteConstant;
import cqrtplm.dto.ClassificationDTO;
import cqrtplm.dto.FlexibleMatchDTO;
import cqrtplm.dto.RTAttributeCardDTO;
import cqrtplm.dto.query.QueryAttributeCardDTO;
import cqrtplm.entity.RTAttributeCardDO;
import cqrtplm.service.RTAttributeCardService;
import cqrtplm.vo.DrawingCodeVO;
import cqrtplm.vo.RTCodeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "属性卡片")
@RequestMapping(RouteConstant.ATTRIBUTECARD_PATH + "v1")
public class RTAttributeCardController {


    @Resource
    private RTAttributeCardService rtAttributeCardService;
    @Autowired
    private TyplmClassCodeCheckExtendServiceImpl typlmClassCodeCheckExtendServiceImpl;
    @Resource
    private TyplmLocalePropertyValueService typlmLocalePropertyValueService;


    @GetMapping("/queryAttrCardDetail")
    @ApiOperation(value = "获取属性卡片的下拉基础信息", produces = "application/json; charset=utf-8")
    public ResponseResult<Map<String, Object>> queryAttrCardDetail() {
        return ResponseResult.success(rtAttributeCardService.queryAttrCardDetail());
    }

    @GetMapping("/queryCategoryCodeAndName")
    @ApiOperation(value = "获取品类编码与名称", produces = "application/json; charset=utf-8")
    public ResponseResult<List<Map<String, Object>>> queryCategoryCodeAndName() {
        return ResponseResult.success(rtAttributeCardService.queryCategoryCodeAndName());
    }


    @GetMapping("/queryAttrCardDetailTWO")
    @ApiOperation(value = "获取属性卡片的基础信息", produces = "application/json; charset=utf-8")
    public ResponseResult<RTAttributeCardDTO> queryAttrCardDetailTOW() {
        RTAttributeCardDTO rtAttributeCardDTO= rtAttributeCardService.queryAttrCardDetailTWO();
        return ResponseResult.success(rtAttributeCardDTO);
    }

    @PostMapping("/insertAttrCard")
    @ApiOperation(value = "新增属性卡片", produces = "application/json; charset=utf-8")
    public ResponseResult<Void> insertAttrCard(@RequestBody RTAttributeCardDTO rtAttributeCardDTO) {
        rtAttributeCardService.insert(rtAttributeCardDTO);
        return ResponseResult.success();
    }

    @PutMapping("/updateAttrCard")
    @ApiOperation(value = "更新属性卡片", produces = "application/json; charset=utf-8")
    public ResponseResult updateAttrCard(@RequestBody RTAttributeCardDTO rtAttributeCardDTO) {
        rtAttributeCardService.updateAttrCard(rtAttributeCardDTO);
        return ResponseResult.success();
    }

    @DeleteMapping("/deleteAttrCardByOid")
    @ApiOperation(value = "批量删除属性卡片", produces = "application/json; charset=utf-8")
    public ResponseResult batchDeleteAttrCard(@RequestBody List<BigInteger> oidList) {
        try {
            rtAttributeCardService.batchDeleteAttrCard(oidList);
        }catch (Exception e) {
            return ResponseResult.fail("400",e.getMessage());
        }

        return ResponseResult.success();
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询属性卡片", produces = "application/json; charset=utf-8")
    public ResponseResult<PageInfo<RTAttributeCardDTO>> list(QueryAttributeCardDTO dto) {
        return ResponseResult.success(rtAttributeCardService.list(dto));
    }

    @GetMapping("/getDetailById")
    @ApiOperation(value = "根据oid查询属性卡片详情", produces = "application/json; charset=utf-8")
    public ResponseResult<RTAttributeCardDO> getDetailById(BigInteger oid) {
        RTAttributeCardDO  rtAttributeCardDO=rtAttributeCardService.getDetailById(oid);
        return ResponseResult.success(rtAttributeCardDO);
    }

    @PostMapping("/flexible-match")
    @ApiOperation(value = "匹配属性卡片分类", produces = "application/json; charset=utf-8")
    public ResponseResult<ClassificationDTO> flexibleMatch(@RequestBody FlexibleMatchDTO dto) {
        ClassificationDTO classificationDO= rtAttributeCardService.flexibleMatch(dto);
        LocalValueVO localValueVO = this.typlmLocalePropertyValueService.queryTypeLocalePropertyValue(classificationDO.getIdentifierRef(),
                                                                                                      "displayName");
        String value = localValueVO.getValue();
        if(StringUtils.isNotBlank( value) && value.contains("已检出")){
            throw RTErrorCodeEnum.DRAWING_CODE_NAME_ERROR.getException("此属性卡片模板正在编辑中，请联系配置管理员");
        }
        return ResponseResult.success(classificationDO);
    }

    @GetMapping({"/queryAttrClassificationTree"})
    @ApiOperation(
            value = "查询属性卡片的分类树",
            notes = "查询属性卡片的分类树",
            produces = "application/json; charset=utf-8"
    )
    public ResponseResult<List<TyplmClassificationEntityTreeVO>> queryClassificationTree(@Param("typeName") String typeName,@Param("categoryCode") String categoryCode) {
        return ResponseResult.success(rtAttributeCardService.queryAttrClassificationTree(typeName, categoryCode));
    }

    @GetMapping("/queryDrawingCode")
    @ApiOperation(value = "查询图样代号", produces = "application/json; charset=utf-8")
    public ResponseResult<List<DrawingCodeVO>> queryDrawingCode(@Param("key") String key) {
        return ResponseResult.success(rtAttributeCardService.queryDrawingCode(key));
    }

    @PostMapping("/queryCode")
    @ApiOperation(value = "查询编码", produces = "application/json; charset=utf-8")
    public ResponseResult<List<RTCodeVO>> queryCode(@RequestBody RTCodeVO postdata) {
        return ResponseResult.success(rtAttributeCardService.queryCode(postdata));
    }

    @GetMapping({"/queryTypeTreeByOid"})
    @ApiOperation("查询对应oid的类型树")
    public ResponseResult queryTypeTreeByOid() {
        List<TypeTree> typeTrees=rtAttributeCardService.queryTypeTreeByOid();
        return ResponseResult.success(typeTrees);
    }

    @GetMapping({"/queryProductSubcategory"})
    @ApiOperation(value = "用产品线查询产品小类", produces = "application/json; charset=utf-8")
    public ResponseResult queryProductSubcategory(@Param("productLine") String productLine) {
        List<String> subcategoryList = rtAttributeCardService.queryProductSubcategory(productLine);
        return ResponseResult.success(subcategoryList);
    }
}